package cn.com.oceansoft.application.listener;

import cn.com.oceansoft.application.Constant;
import cn.com.oceansoft.application.common.spring.SpringContextHolder;
import cn.com.oceansoft.application.common.util.HttpClientUtil;
import cn.com.oceansoft.application.constant.ConfigInfo;
import cn.com.oceansoft.framework.kits.ApiResult;
import cn.com.oceansoft.modules.cache.service.BaseCacheService;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.entity.ContentType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by zhangxh on 2016/11/9.
 */
public class BaseSessionListener implements HttpSessionListener {
    private static Logger logger = LoggerFactory.getLogger(BaseSessionListener.class);
    @Override
    public void sessionCreated(HttpSessionEvent httpSessionEvent) {
        BaseCacheService cache= SpringContextHolder.getBean("baseCacheService");
        Constant.count.incrementAndGet();
        cache.incr(Constant.ONLINE_NUM);
        cache.incr(Constant.TOTAL_VISITOR_NUM);




        /*String total=cache.get(Constant.TOTAL_VISITOR_NUM);//先redis中获取,获取不到去数据库获取
        if(StringUtils.isEmpty(total)) {//只要redis服务器不去清空数据下面代码基本不会执行到
            try {
                String result = HttpClientUtil.doGet(configInfo.getMsHostUrl() + "dicts/key/" + Constant.TOTAL_VISITOR_NUM);
                ApiResult apiResult = JSONObject.parseObject(result, ApiResult.class);
                List list = (List) apiResult.getData();
                JSONObject json = (JSONObject) list.get(0);
                total=json.get("val").toString();
            } catch (IOException e) {
                e.printStackTrace();
                logger.error("获取访问量失败");
            }
        }*/
        /*if(Integer.parseInt(total)%10000==0){//满10000存一次，防redis服务器出问题
            Map map=new HashMap();
            map.put("val",total);
            String patchResult=HttpClientUtil.doPatch(configInfo.getMsHostUrl()+"dicts/"+Constant.TOTAL_VISITOR_NUM,map, ContentType.APPLICATION_FORM_URLENCODED);
            ApiResult apiResult1 = JSONObject.parseObject(patchResult, ApiResult.class);
            if(!apiResult1.isSucc()){
                logger.error("更新访问量失败");
            }
        }*/
    }


    @Override
    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
        BaseCacheService cache= SpringContextHolder.getBean("baseCacheService");
        cache.decr(Constant.ONLINE_NUM);
        Constant.count.decrementAndGet();
    }
}
